package com.jf.cloud.user.controller.app;

import com.jf.cloud.user.model.UserTravels;
import com.jf.cloud.user.service.UserService;
import com.jf.cloud.user.service.UserTravelsService;
import com.jf.cloud.user.vo.UserTravelsVO;
import com.jf.cloud.user.dto.UserTravelsDTO;

import com.jf.cloud.api.auth.bo.UserInfoInTokenBO;
import com.jf.cloud.common.database.dto.PageDTO;
import com.jf.cloud.common.database.vo.PageVO;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.common.security.AuthUserContext;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import ma.glasnost.orika.MapperFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;

/**
 * 
 *
 * @author zz
 * @date 2023-11-09 09:50:04
 */
@RestController("appUserTravelsController")
@RequestMapping("/user_travels")
@Tag(name = "用户游记")
public class UserTravelsController {

    @Autowired
    private UserTravelsService userTravelsService;

    @Autowired
    private MapperFacade mapperFacade;

    @Autowired
    private UserService userService;

    @GetMapping("/page")
    @Operation(summary = "获取列表", description = "分页获取列表")
    public ServerResponseEntity<PageVO<UserTravels>> page(@Valid PageDTO pageDTO) {
        PageVO<UserTravels> userTravelsPage = userTravelsService.page(pageDTO);
        return ServerResponseEntity.success(userTravelsPage);
    }

    @GetMapping("/list")
    @Operation(summary = "获取列表", description = "分页获取列表")
    public ServerResponseEntity<PageVO<UserTravels>> list(@Valid PageDTO pageDTO,@RequestParam Long userId) {

        if(userId==0){
            userId = AuthUserContext.get().getUserId();
        }
        PageVO<UserTravels> userTravelsPage = userTravelsService.list(pageDTO,userId);
        return ServerResponseEntity.success(userTravelsPage);
    }


    @GetMapping
    @Operation(summary = "获取", description = "根据id获取")
    public ServerResponseEntity<UserTravels> getById(@RequestParam Long id) {
        UserTravels userTravels=userTravelsService.getById(id);
        userTravels.setNickName(userService.getByUserId(userTravels.getUserId()).getNickName());
        userTravels.setImg(userService.getByUserId(userTravels.getUserId()).getPic());
        return ServerResponseEntity.success(userTravels);
    }

    @PostMapping
    @Operation(summary = "保存", description = "保存")
    public ServerResponseEntity<Void> save(@Valid @RequestBody UserTravelsDTO userTravelsDTO) {
        Long userId = AuthUserContext.get().getUserId();
        userTravelsDTO.setUserId(userId);
        UserTravels userTravels = mapperFacade.map(userTravelsDTO, UserTravels.class);
        userTravelsService.save(userTravels);
        return ServerResponseEntity.success();
    }

    @PutMapping
    @Operation(summary = "更新", description = "更新")
    public ServerResponseEntity<Void> update(@Valid @RequestBody UserTravelsDTO userTravelsDTO) {
        Long userId = AuthUserContext.get().getUserId();
        userTravelsDTO.setUserId(userId);
        UserTravels userTravels = mapperFacade.map(userTravelsDTO, UserTravels.class);
        userTravelsService.update(userTravels);
        return ServerResponseEntity.success();
    }

    @DeleteMapping
    @Operation(summary = "删除", description = "根据id删除")
    public ServerResponseEntity<Void> delete(@RequestParam Long id) {
        userTravelsService.deleteById(id);
        return ServerResponseEntity.success();
    }
}
